package com.hr.builder;

import java.util.List;

import org.springframework.stereotype.Component;

import com.espirit.eap.manager.I18n;
import com.espirit.eap.manager.ModelBuilderSupport;
import com.espirit.eap.manager.RelatedModel;
import com.espirit.eap.manager.TabbedModel;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.FunctionButton;
import com.espirit.eap.pagelayout.LinkedButton;
import com.espirit.eap.pagelayout.ModelField;
import com.espirit.eap.sa.Employee;
import com.hr.Salary;
import com.hr.SalaryAcctItem;
import com.hr.SalaryItem;

@Component
public class EmployeeBuilder extends ModelBuilderSupport {
	static final String CODE = "code";
	static final String NAME = "name";
	static final String TEL = "tel";
	static final String EMAIL = "email";
	static final String MOBILE = "mobile";
	static final String DEPARTMENT = "department";
	static final String POSITION = "position";
	static final String FIXEDSALARY = "fixedSalary";
	static final String ACTIVE = "active";
	static final String BANKNBR = "bankNbr";
	static final String DEPTIDS = "deptIds";
	static final String SEX = "sex";
	static final String EDUCATION = "education";
	static final String IDCARD = "idCard";
	static final String IDCARDADDRESS = "idCardAddress";
	static final String ADDRESS = "address";
	
	static final String IDTYPE = "idType";
	static final String SOCIALTYPE = "socialType";
	static final String SUBSIDY = "subsidy";
	static final String DUTYCARDNBR = "dutyCardNbr";
	
	static final String ATTRIBUTE = "attribute";
	static final String HOURSALARY = "hourSalary";
	
	static final String ENTRYDATE = "entryDate";
	static final String USERID = "userId";
	
	@Override
	public boolean init() {
		return super.setClass(Employee.class, "Employee", "职员信息");
	}

	@Override
	public void buildFields() {
		super.buildFields();
		addModelField(CODE, "Code", "员工编号");
		addModelField(NAME, "Name", "姓名");
		addModelField(TEL, "Tel", "电话");
		addModelField(EMAIL, "Email", "邮箱");
		addModelField(MOBILE, "Mobile", "手机");
		addModelField(POSITION, "Position", "职位");
		addModelField(DEPARTMENT, "Department", "部门");
		addModelField(DEPTIDS, "Department", "部门");
		addModelField(FIXEDSALARY, "Fixed Salary", "基本工资");
		addModelField(ACTIVE, "Active", "状态");
		addModelField(BANKNBR, "Bank Nbr", "银行账号");
		addModelField(SEX, "Sex", "性别");
		addModelField(EDUCATION, "Education", "学历");
		addModelField(IDCARD, "ID Card", "身份证");
		addModelField(IDCARDADDRESS, "ID Card Address", "身份证地址");
		addModelField(ADDRESS, "Address", "现住址");
		
		addModelField(IDTYPE, "IDType", "身份证性质");
		addModelField(SOCIALTYPE, "SocialType", "社保类型");
		addModelField(SUBSIDY, "Subsidy", "津贴");
		addModelField(DUTYCARDNBR, "Dutycard Nbr", "考勤卡编号");
		
		addModelField(ATTRIBUTE, "Attribute", "劳动者身份");
		addModelField(HOURSALARY, "Hour Salary", "时薪");
		
		addModelField(ENTRYDATE, "Entry Date", "入职日期");
		
		addModelField(USERID, "USER ID", "User Id");
	}
	
	@Override
	protected void setDefaultLayoutButtons() {
		super.setDefaultLayoutButtons();
		addButton(FunctionButton.IMPORT);
	}
	
	@Override
	protected void addSearchFields() {
		super.addSearchField(convertPropertyToField( NAME, DEPTIDS, ACTIVE));
	}
	
	@Override
	protected void addListFields() {
		LinkedButton lb = createActiveLinkedButton();
		listElements.add(lb);
		addListField(convertPropertyToField(CODE, NAME, DEPARTMENT, DUTYCARDNBR, SEX, EDUCATION, POSITION, ENTRYDATE, BANKNBR, MOBILE, IDTYPE, SOCIALTYPE, ATTRIBUTE, USERID));
	}
	
	@Override
	protected void addDetailFields() {
		addDetailField(convertPropertyToField(CODE, NAME, ACTIVE, DEPTIDS, DUTYCARDNBR, IDCARD, IDTYPE, SOCIALTYPE, ATTRIBUTE, SEX, EDUCATION, POSITION, ENTRYDATE, FIXEDSALARY, HOURSALARY, SUBSIDY, BANKNBR, MOBILE, TEL, EMAIL, IDCARDADDRESS, ADDRESS, USERID));
	}
	
	protected void buildDetailLayout() {
		// create empty detail layout
		getDefaultDetailLayout();
	}
	
	@Override
	protected void preBuildMiscDatas() {
		I18n baseName = i18nService.addI18n("基本信息", "Basic Info");
		I18n salaryName = i18nService.addI18n("工资信息", "Repl Info");
		
		FlatSection basicInfo = getDefaultDetailLayout().createFlatSection();
		basicInfo.setI18n(baseName.getKey());
		basicInfo.setNamePrefix("model");
		basicInfo.setColsPerRow(2);
		basicInfo.addExProperty("vertical", "true");
		
		List<ModelField> baseFields = convertPropertyToField(CODE, NAME, ACTIVE, DEPTIDS, DUTYCARDNBR, IDCARD, IDTYPE, SOCIALTYPE, ATTRIBUTE, SEX, EDUCATION, POSITION, ENTRYDATE, MOBILE, TEL, EMAIL, IDCARDADDRESS, ADDRESS);
		elementService.addSectionElement(basicInfo, convertFieldToElement(baseFields, false));
		
		FlatSection salaryInfo = getDefaultDetailLayout().createFlatSection();
		salaryInfo.setI18n(salaryName.getKey());
		salaryInfo.setNamePrefix("model");
		salaryInfo.setColsPerRow(2);
		salaryInfo.addExProperty("vertical", "true");
		List<ModelField> salaryFields = convertPropertyToField(FIXEDSALARY, HOURSALARY, SUBSIDY, BANKNBR);
		elementService.addSectionElement(salaryInfo, convertFieldToElement(salaryFields, false));
	}
	
	protected RelatedModel[] getRelatedModels(){
		RelatedModel getLayout = new RelatedModel(this.clazz, "getLayout");
		return new RelatedModel[]{getLayout};
	}
	
	protected TabbedModel[] getTabbedModels(){
		TabbedModel salaryAcctItems = new TabbedModel(SalaryAcctItem.class, SEARCHRELATEDRESULT);
		TabbedModel salaryItem = new TabbedModel(SalaryItem.class, SEARCH, "employee");
		TabbedModel salary = new TabbedModel(Salary.class, SEARCH, "employee");
//		TabbedModel salaryTickets = new TabbedModel(SalaryTicket.class, SEARCH, "employee");
		
		return new TabbedModel[]{salaryAcctItems, salary, salaryItem};
	}
	
	@Override
	protected void gatherExproperties() {
		exProperties.put(REQUIRED, new String[]{NAME, ACTIVE, DEPTIDS, IDTYPE, SOCIALTYPE, ATTRIBUTE});
	}
}